<?php
class ui_input_date_select extends ui_input
{
	function __construct($name,$value=null,$show_hours=false,$show_minutes=false)
	{
		global $ti;
		$this->init(__FILE__,'div');
		$this->name = $name;
		$this->date_type = $ti->config['var.date_type'];
		
		$this->start_year = 1970;
		$this->end_year   = 2020;
		
		$this->show_hours   = $show_hours;
		$this->show_minutes = $show_minutes;
		
		$this->add(new ui_input_hidden($name,$value));
		
		$this->add(new ui_input_select($this->name.'_year'));
		$this->last->set('onchange','ti.forms.dateSelect(this);');
		$years = array();
		for ($i = $this->start_year; $i < $this->end_year; $i++)
		{
			$years[$i.''] = $i.'';
		}
		
		$this->last->source=$years;
		$this->year = $this->last;
		$this->year->check_data_value = false;
		
		$this->add(new ui_input_select($this->name.'_month'));
		$this->last->set('onchange','ti.forms.dateSelect(this);');
		$this->last->source=array(
			'01'=>'01',
			'02'=>'02',
			'03'=>'03',
			'04'=>'04',
			'05'=>'05',
			'06'=>'06',
			'07'=>'07',
			'08'=>'08',
			'09'=>'09',
			'10'=>'10',
			'11'=>'11',
			'12'=>'12',
		);
		$this->month = $this->last;
		$this->month->check_data_value = false;
		
		$this->add(new ui_input_select($this->name.'_day'));
		$this->last->set('onchange','ti.forms.dateSelect(this);');
		$this->last->source=array(
			'01'=>'01',
			'02'=>'02',
			'03'=>'03',
			'04'=>'04',
			'05'=>'05',
			'06'=>'06',
			'07'=>'07',
			'08'=>'08',
			'09'=>'09',
			'10'=>'10',
			'11'=>'11',
			'12'=>'12',
			'13'=>'13',
			'14'=>'14',
			'15'=>'15',
			'16'=>'16',
			'17'=>'17',
			'18'=>'18',
			'19'=>'19',
			'20'=>'20',
			'21'=>'21',
			'22'=>'22',
			'23'=>'23',
			'24'=>'24',
			'25'=>'25',
			'26'=>'26',
			'27'=>'27',
			'28'=>'28',
			'29'=>'29',
			'30'=>'30',
			'31'=>'31',
		);
		$this->day = $this->last;
		$this->day->check_data_value = false;
		
		if($this->show_hours)
		{
			$this->add(new ui_input_select($this->name.'_hour'));
			$this->last->set('onchange','ti.forms.dateSelect(this);');
			$this->last->source=array(
				'00'=>'00',
				'01'=>'01',
				'02'=>'02',
				'03'=>'03',
				'04'=>'04',
				'05'=>'05',
				'06'=>'06',
				'07'=>'07',
				'08'=>'08',
				'09'=>'09',
				'10'=>'10',
				'11'=>'11',
				'12'=>'12',
				'13'=>'13',
				'14'=>'14',
				'15'=>'15',
				'16'=>'16',
				'17'=>'17',
				'18'=>'18',
				'19'=>'19',
				'20'=>'20',
				'21'=>'21',
				'22'=>'22',
				'23'=>'23',
			);
			$this->hour = $this->last;
			$this->hour->check_data_value = false;
		}
		
		if($this->show_minutes)
		{
			$this->add(new ui_input_select($this->name.'_minute'));
			$this->last->set('onchange','ti.forms.dateSelect(this);');
			$this->last->source=array(
				'00'=>'00',
				'01'=>'01',
				'02'=>'02',
				'03'=>'03',
				'04'=>'04',
				'05'=>'05',
				'06'=>'06',
				'07'=>'07',
				'08'=>'08',
				'09'=>'09',
				'10'=>'10',
				'11'=>'11',
				'12'=>'12',
				'13'=>'13',
				'14'=>'14',
				'15'=>'15',
				'16'=>'16',
				'17'=>'17',
				'18'=>'18',
				'19'=>'19',

				'20'=>'20',
				'21'=>'21',
				'22'=>'22',
				'23'=>'23',
				'24'=>'24',
				'25'=>'25',
				'26'=>'26',
				'27'=>'27',
				'28'=>'28',
				'29'=>'29',

				'30'=>'30',
				'31'=>'31',
				'32'=>'32',
				'33'=>'33',
				'34'=>'34',
				'35'=>'35',
				'36'=>'36',
				'37'=>'37',
				'38'=>'38',
				'39'=>'39',

				'40'=>'40',
				'41'=>'41',
				'42'=>'42',
				'43'=>'43',
				'44'=>'44',
				'45'=>'45',
				'46'=>'46',
				'47'=>'47',
				'48'=>'48',
				'49'=>'49',

				'50'=>'50',
				'51'=>'51',
				'52'=>'52',
				'53'=>'53',
				'54'=>'54',
				'55'=>'55',
				'56'=>'56',
				'57'=>'57',
				'58'=>'58',
				'59'=>'59',
			);
			$this->minute = $this->last;
			$this->minute->check_data_value = false;
		}
	}
	
	function pre_render($data)
	{
		global $ti;
		parent::pre_render($data);
		$value = $this->get('value');
		#$ti->log('here we go: '.print_r($data,true));	
		
		if($this->date_type == 'timestamp')
		{
			$mydate = date_parse($value);
			$mydate['mday'] = $mydate['day'];
			$mydate['mon']  = $mydate['month'];
			$mydate['hours'] = $mydate['hour'];
			$mydate['minutes'] = $mydate['minute'];
		}
		if($this->date_type == 'int')
		{
			$mydate = getdate(intval($value));
		}
		
		$this->year->set('value',$mydate['year']);		
		$this->month->set('value',str_pad($mydate['mon'],2,'0',STR_PAD_LEFT));		
		$this->day->set('value',str_pad($mydate['mday'],2,'0',STR_PAD_LEFT));
		if($this->hour)
		{
			$this->hour->set('value',str_pad($mydate['hours'],2,'0',STR_PAD_LEFT));
		}
		if($this->minute)
		{
			$this->minute->set('value',str_pad($mydate['minutes'],2,'0',STR_PAD_LEFT));
		}
		
		if($this->form)
		{
			if(!$this->form->allow_edit)
			{
				$this->render = false;
				$this->post_outer_html .= $this->get('value');
			}
		}
	}
}
?>